package com.epam.web_project.bank.command.admin_commands;

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.customer_dao.CustomerDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.dao.transaction_dao.TransactionDAO;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.entity.Transaction;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class PutMoneyCommand implements Command {

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		BigDecimal amount=new BigDecimal(request.getParameter("amount"));
		int accountId=Integer.valueOf(request.getParameter("accId"));
		Transaction transaction = new Transaction();
		MySQLDAOFactory daoFactory=new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		CustomerDAO customerDAO=daoFactory.getCustomerDAO();
		Customer admin = customerDAO.findByLogin("admin");
		transaction.setAccountFromId(admin.getAccounts().get(0).getId());
		transaction.setAccountToId(accountId);
		transaction.setAmountSent(amount);
		TransactionDAO transactionDAO = daoFactory.getTransactionDAO();
		String message = transactionDAO.adminSave(transaction);
		HttpSession session = request.getSession();
		session.setAttribute("message", message);
		AppPropertiesLoader props=AppPropertiesLoader.getInstance();
		return props.getProperty("show_working_accounts");
	}

}
